package com.wish.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.wish.dto.BusinessDto;
import com.wish.entity.*;
import com.wish.service.IBusinessService;
import com.wish.service.ISysDeptService;
import com.wish.service.ISysRoleService;
import com.wish.service.ISysUserService;
import com.wish.util.Upload;
import com.wish.vo.ResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商户表 前端控制器
 * </p>
 *
 * @author 五组
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/wish/business")
public class BusinessController {
    @Resource
    ISysUserService userService;

    @Resource
    ISysDeptService deptService;

    @Resource
    ISysRoleService roleService;
    @Autowired
    private IBusinessService businessService;
    //列表
    @RequestMapping("getList")
    public Page<Business> getList(@RequestBody BusinessVo businessVo){
        Page<Business> page = new Page<>();
        page.setCurrent(businessVo.getPageNum());
        page.setSize(businessVo.getPageSize());
        Page<Business> pageInfo = businessService.getList(page);
        return pageInfo;
    }

    @RequestMapping("add")
    public ResultEntity add(@RequestBody Business business){
        business.setCreateTime(new Date());
        business.setStatus(3);
        System.out.println(business+"-=-=-=-=-=-=");
        boolean b = businessService.save(business);
        return ResultEntity.ok(b);
    }

    @RequestMapping("upload")
    public String upload(MultipartFile file) throws IOException {
        return "http://localhost:10001/"+Upload.upload(file,"C:\\pic\\");
    }
    @RequestMapping("list")
    public PageInfo<Business> list(@RequestBody BusinessDto dto){
        return businessService.tolist(dto);

    }

    /**
     *  审核未通过
     * @param dto
     * @return
     */
    @RequestMapping("update")
    public boolean update(@RequestBody Business dto){
        dto.setStatus(2);
        businessService.update(dto,new QueryWrapper<Business>().eq("id",dto.getId()));
        return true;
    }

    /*
     * 审核成功
     * */
    @RequestMapping("update2")
    public ResultEntity update2(@RequestBody Business dto){
        /*HashMap<String, Object> map = new HashMap<>();
        map.put("businessId",dto.getId());*/

        /*System.out.println("555555555"+dto.getId());*/
        /*
         * 部门获取商户id返回到集合中
         * 进行判断如果集合长度为空
         * 就返回
         * 不为空就进行
         * */
        List<SysDept> deptlist = deptService.queryList2(dto.getId());
        if (CollectionUtils.isEmpty(deptlist) || deptlist.size() == 0){
            return ResultEntity.ok("200","请在部门下添加商户",null);
        }

        /*map.put("deptId",deptlist.get(0).getDeptId());*/
        /*
         * 角色根据部门获取商户id
         * */
        List<SysRole> roleList  = roleService.queryList(deptlist.get(0).getDeptId(),dto.getId());
        if (CollectionUtils.isEmpty(roleList)){
            return ResultEntity.ok("200","请在角色下添加商户",null);
        }
        /*
         * 用户根据部门获取商户id
         * 如果集合为空格就返回
         *
         * */
        List<SysUser> userList = userService.queryLists(deptlist.get(0).getDeptId(),dto.getId());
        if (CollectionUtils.isEmpty(userList)){
            return ResultEntity.ok("200","请在用户下添加商户",null);
        }else {
            List<Long> list = new ArrayList<>();
            for (int i = 0 ; i < roleList.size(); i++){
                SysRole sysRole = roleList.get(i);
                list.add(sysRole.getRoleId());
            }
            SysUser user = userList.get(0);
            user.setRoleId(list);
            user.setApprovalFlag(1);
            userService.updateUser(user);
        }
        dto.setStatus(1);
        businessService.update(dto,new QueryWrapper<Business>().eq("id",dto.getId()));
        return ResultEntity.ok("200","审核成功",dto);
    }


}
